package com.skcc.cip.ngms.mail.processor;

import javax.servlet.http.HttpServletResponse;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.util.ExchangeHelper;

import com.skcc.cip.ngms.common.NMPConstant;

public class MailNotificationResultResponse implements Processor {

	private static final StringBuilder responseXML = new StringBuilder(); 
	
	public void process(Exchange exchange) throws Exception {
		exchange.getIn().setBody(exchange.getProperty("originRequest"));
//		HttpServletResponse response = exchange.getUnitOfWork().getOriginalInMessage().getBody(HttpServletResponse.class);
		HttpServletResponse response = exchange.getIn().getBody(HttpServletResponse.class);
		
		String str = getResponseXML(NMPConstant.SUBSCRIBE_OK);
		
		response.setCharacterEncoding("UTF-8");
		response.setStatus(HttpServletResponse.SC_OK);
		response.setContentType("text/xml; charset=UTF-8");
		response.setContentLength(str.length());
		response.getOutputStream().print(str); 	
	}
		
	private static String getResponseXML(String result) {
		if (responseXML.length() == 0) {
			responseXML.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
			responseXML.append("<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">\n");
			responseXML.append("  <s:Body>\n");
			responseXML.append("	<SendNotificationResult xmlns=\"http://schemas.microsoft.com/exchange/services/2006/messages\">\n");
			responseXML.append("		<SubscriptionStatus>%1$s</SubscriptionStatus>\n");
			responseXML.append("	</SendNotificationResult>\n");
			responseXML.append("  </s:Body>\n");
			responseXML.append("</s:Envelope>\n");
		}
		return String.format(responseXML.toString(), result);
	}

}
